package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CarDispatchMapper;
import com.qf.ssm.mapper.CarManagementMapper;
import com.qf.ssm.po.CarDispatchRecord;
import com.qf.ssm.qo.CarDispatchQo;
import com.qf.ssm.qo.CarDispatchRecodeQo;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarDispatchService;
import com.qf.ssm.utlis.CommonBeanutils;
import com.qf.ssm.vo.CarAndStoreInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;


@Service
public class CarDispatchServiceImpl implements CarDispatchService {

    @Autowired
    private CarDispatchMapper carDispatchMapper;
    @Autowired
    private CarManagementMapper carManagementMapper;

    @Override
    public PageInfo<CarAndStoreInfoVo> infoList(int page, int size, CarDispatchQo carDispatchQo) {
        PageInfo<CarAndStoreInfoVo> carAndStoreInfoVoPageInfo = CommonBeanutils.convertPageInfo(PageHelper.startPage(page, size).doSelectPageInfo(() -> carDispatchMapper.selectCarInfoAndStoreList(carDispatchQo)),
                new PageInfo<>(), CarAndStoreInfoVo::new);
        if (ObjectUtils.isEmpty(carAndStoreInfoVoPageInfo.getList())){
            throw new ServiceException(ResultStatus.INFO_NO_EXIST);
        }
        return carAndStoreInfoVoPageInfo;

    }


    @Transactional
    @Override
    public Integer carDispatchBegin(CarDispatchRecodeQo carDispatchRecodeQo) {

        int count = carDispatchMapper.updateEmpStatusById(carDispatchRecodeQo.getEmp_id(), 1);
        if (count<1){
            throw new ServiceException(ResultStatus.STATUS_UPDATE_ERROR);
        }

        count = carDispatchMapper.insertCarDispatchRecode(carDispatchRecodeQo.getEmp_id(), carDispatchRecodeQo.getCid(),carDispatchRecodeQo.getOrigin(),carDispatchRecodeQo.getDestination());
        if (count<1){
            throw new ServiceException(ResultStatus.DispatchRecode_ADD_ERROR);
        }
        return 1;
    }


    @Transactional
    @Override

    public Integer carDispatchFinish(Integer did) {
        long currentTimeMillis = System.currentTimeMillis();
        Date date=new Date(currentTimeMillis);
        int count = carDispatchMapper.updateCarDispatchStatusById(did, date, 1);
        if (count<1){
            throw new ServiceException(ResultStatus.DispatchRecode_UPDATE_ERROR);
        }
        CarDispatchRecord carDispatchRecord = carDispatchMapper.selectCarDispatchRecodeByid(did);
         count = carDispatchMapper.updateEmpStatusById(carDispatchRecord.getEmpId(), 0);
        if (count<0){
            throw new ServiceException(ResultStatus.STATUS_UPDATE_ERROR);
        }
         count= carDispatchMapper.updateCarStoreId(carDispatchRecord.getCid(), carDispatchRecord.getDestination());
        if (count<0){
            throw new ServiceException(ResultStatus.CAR_UPDATE_STOREID_ERROR);
        }

        return count;
    }


}
